class ProfileController < ApplicationController
  
  def personal
  end
  
  def remove
  end
  
  def picture
    @face=Face.new
  end  
  
  def email
    @emails=@user.emails
  end
  
  def update
    if request.post?
      @user.fullname=params[:user][:fullname]
      @user.save!
      flash[:notice]=info(0,"Your personal info is updated.")
      redirect_to :action=>"personal"
    end
  end
  
  def resend
    if request.post?
      email_item=Email.find(:first, :conditions=>["email=? ", params[:email]])
      if email_item.nil?
        flash[:notice]=info(1,"Please check your email")
      else
        flash[:notice]=info(0,"Verification has been sent to <b>"+email_item.email+"</b>")
        email_item.generate_hash
        email_item.save
      end
      redirect_to :action=>'email'
    end
  end
 
  def create
    if (Email.validate(params[:email])==2)
      @user.emails<<Email.new(:email=>params[:email])
      @user.save
    end
    flash[:notice]=info(0,"Please click the link in the verification email.")
    redirect_to :action=>'email'
  end
  
  def remove_email
    if request.post?
      email=Email.find_by_email(params[:email])
      if !(email.nil?)&&email.user_id==@user.id
        email.destroy
      end
      flash[:notice]=info(0,"Your Email <b>"+params[:email]+"</b> has been removed.")
      redirect_to :action=>'email'
    end
  end
  
  def remove
    if request.post?
      if params[:confirm_info]=="REMOVE MY ACCOUNT"
        @user.destroy
        flash[:notice]=info(0,"You've permernantly removed your account.")
        session[:user_id]=nil
        redirect_to :controller=>"account", :action=>"start"
      else
        flash[:notice]=info(2,"Your typed message doesn't match with the predifiend phrase.")
      end
    end
  end
  
  def set_major
    if request.post?
      email=params[:email]
      emails=@user.emails
      emails.each do |t|
        if t.email==email
          t.is_major=1
          @user.email=email
          @user.save
        else
          t.is_major=0
        end
        t.save
      end
      flash[:notice]=info(0,"<b>"+@user.major_email.email+"</b> is set to your primary email")
      redirect_to :action=>'email'
     end
    end
  
  def save_face
    if request.post?
      @face=Face.new(params[:face])
      @user.face=@face
      
      begin
        @face.save!
      rescue ActiveRecord::RecordInvalid
        redirect_to(:action=>'picture')
        flash[:notice]=info(2,show_errors(@face))
      else
        redirect_to(:action=>'picture')
        flash[:notice]=info(0,"You've successfully update your avatar")      
      end
    end
  end  
  
end
